package net.wanho;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.google.common.base.CaseFormat;

import java.util.Map;

public class CodeGenerator {

    private static final String projectPath = "D:\\qz126\\code\\spring\\shop-parent\\shop-backend";

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator generator = new AutoGenerator();

        // 1、全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("猛虎王");
        gc.setOpen(false);
        gc.setFileOverride(true); // 重新生成时文件是否覆盖
        gc.setServiceName("%sService"); // Service接口的命名方式
        gc.setSwagger2(true);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        generator.setGlobalConfig(gc);

        // 2、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/shop?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        generator.setDataSource(dsc);

        // 3、包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("net.wanho.shop");
        pc.setEntity("po"); // entity 层
        pc.setMapper("mapper"); // mapper 层
        pc.setService("service"); // service层
        pc.setController("controller"); // controller 层
        generator.setPackageInfo(pc);

        // 4、自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // mapper模板
        String templatePath = "/templates/mapper.xml.btl";

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix("sp_"); // 过滤表前辍
        strategy.setNaming(NamingStrategy.underline_to_camel); //数据库表映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); //数据库列映射到实体属性的命名策略
        strategy.setEntityLombokModel(true);  // lombok 模型
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
        generator.setStrategy(strategy);


        // 6、设置解析模板
        generator.setTemplateEngine(new BeetlTemplateEngine() {

            // 获取主键
            String primaryKey = "";
            String primaryKeyUnderline = "";

            @Override
            public AbstractTemplateEngine init(ConfigBuilder configBuilder) {
                AbstractTemplateEngine engine = super.init(configBuilder);
                // 映射文件放到resources目录下
                configBuilder.getPathInfo().put("xml_path", projectPath + "/src/main/resources/mapper/" + pc.getModuleName());
                return engine;
            }

            @Override
            public Map<String, Object> getObjectMap(TableInfo tableInfo) {
                for (TableField tableField : tableInfo.getFields()) {
                    if (tableField.isKeyFlag()) {
                        primaryKey = tableField.getPropertyName();
                        primaryKeyUnderline = tableField.getName();
                        break;
                    }
                }

                // 获取源码中的map
                Map<String, Object> objectMap = super.getObjectMap(tableInfo);
                // 额外添加两个键值对
                objectMap.put("entityCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, tableInfo.getEntityName()));
                objectMap.put("primaryKey", primaryKey);
                objectMap.put("primaryKeyUnderline", primaryKeyUnderline);
                return objectMap;
            }
        });
        // 7、执行生成
        generator.execute();
    }

}
